<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$user_id = $user->getUserId();

$query = "select `sname`,`name`,`tname`,`login`,`email`,`birthdate`,`city_id`,`about`,`sex` from `users` where `user_id` = '$user_id' limit 1";
$profile = $db->getRow($query);

require_once CABINET.DS."head.php";
?>
<script type='text/javascript'>
$(document).ready(function(){
	$("#avatar img").click(function(){
		$("#avatarUpload").click();
	})
	$("#avatarUpload").change(function(){
		$("#avatarForm").submit();
	});
	
	var dateOptions = {
		dateFormat: 'dd.mm.yy',
		showAnim: 'drop',
		dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
		monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		firstDay: 1
	};
	$("#birthdate").datepicker(dateOptions);
})
</script>
<div class="full">
	<div id="accountsettings">
		<div id="avatar">
			<?php echo drawAva($user_id,"big"); ?>
			<div style="clear:left;line-height: 16pt; float:left">
				<form method='post' action='/cabinet/profile/edit' id='avatarForm' enctype="multipart/form-data">
					<input type='hidden' name='what' value='avatar' />
					<p>Загрузить новый</p>
					<input type='file' name='avatar' id='avatarUpload' />
					<noscript><input type='submit' value='Загрузить' /></noscript>
				</form>
			</div>
		</div>
		<div id="fio"><?php echo $profile['sname']." ".$profile['name']." ".$profile['tname']; ?></div>
		<div id="editaccountinfo">
			<table>
				<tr>
					<td class="tLabel"><label for="login">Логин</label></td>
					<td class="tInput"><span><?php echo $profile['login']; ?></span></td>
					<td class="tButton"></td>
				</tr>
				<tr>
					<td class="tLabel"><label for="email">E-mail</label></td>
					<td class="tInput"><span><?php echo $profile['email']; ?></span></td>
					<td class="tButton"></td>
				</tr>
			</table>
			<form method='post' action='/cabinet/profile/edit'>
				<input type='hidden' name='what' value='birthdate' />
				<table>
					<tr>
						<td class="tLabel"><label for="birthdate">Дата рождения</label></td>
						<td class="tInput">
							<input type="text" class='rBorder' name="birthdate" id="birthdate" value='<?php echo date("d.m.Y",$profile['birthdate']); ?>' /><?php
							if ($route->GET("birthdate") == "success")
								echo "<p class='inf good'>успешно обновлено<p>";
							elseif ($route->GET("birthdate") == "fail")
								echo "<p class='inf bad'>не удалось обновить<p>";
							?>
						</td>
						<td class="tButton"><input type="submit" value="Изменить"></td>
					</tr>
				</table>
			</form>
			<form method='post' action='/cabinet/profile/edit'>
				<input type='hidden' name='what' value='city' />
				<table>
					<tr>
						<td class="tLabel"><label for="city">Город проживания</label></td>
						<td class="tInput">
							<select name="city" id="city" class='rBorder'><?php
								$oren = $db->getCell("select `city_id` from `cities` where `name` like 'Оренбург' limit 1");
								$not_in_ob = $db->getCell("select `city_id` from `cities` where `name` like 'Не в Оренбургской области' limit 1");
								$cities = $db->getTable("select * from `cities` where `city_id` != '$oren' and `city_id` != '$not_in_ob' order by `name`");
								echo "<option value='$oren'>Оренбург</option>";
								echo selectCreate($cities,"city_id","name",$profile['city_id']);
								echo "<option value='$not_in_ob'>Не в Оренбургской области</option>";
							?></select><?php
							if ($route->GET("city") == "success")
								echo "<p class='inf good'>успешно обновлено<p>";
							elseif ($route->GET("city") == "fail")
								echo "<p class='inf bad'>не удалось обновить<p>";
							?>
						</td>
						<td class="tButton"><input type="submit" value="Изменить"></td>
					</tr>
				</table>
			</form>
			<form method='post' action='/cabinet/profile/edit'>
				<input type='hidden' name='what' value='about' />
				<table>
					<tr>
						<td class="tLabel"><label for="about">О себе</label></td>
						<td class="tInput">
							<textarea name="about" class='rBorder' id="about"><?php echo $profile['about']; ?></textarea><?php
							if ($route->GET("about") == "success")
								echo "<p class='inf good'>успешно обновлено<p>";
							elseif ($route->GET("about") == "fail")
								echo "<p class='inf bad'>не удалось обновить<p>";
							?>
						</td>
						<td class="tButton"><input type="submit" value="Изменить"></td>
					</tr>
				</table>
			</form>
			<form method='post' action='/cabinet/profile/edit'>
				<input type='hidden' name='what' value='sex' />
				<table>
					<tr>
						<td class="tLabel"><label for="male">Пол</label></td>
						<td class="tInput">
							<input type="radio" name="sex" value='male' id="male" <?php if ($profile['sex'] == 'male') echo "checked='checked'"; ?> /><label for="male">Мужской</label>
							<input type="radio" name="sex" value='female' id="female" <?php if ($profile['sex'] == 'female') echo "checked='checked'"; ?> /><label for="female">Женский</label><?php
							if ($route->GET("sex") == "success")
								echo "<p class='inf good'>успешно обновлено<p>";
							elseif ($route->GET("sex") == "fail")
								echo "<p class='inf bad'>не удалось обновить<p>";
							?>
						</td>
						<td class="tButton"><input type="submit" value="Изменить"></td>
					</tr>
				</table>
			</form>
			<form method='post' action='/cabinet/profile/edit'>
				<input type='hidden' name='what' value='password' />
				<table>
					<tr>
						<td style="padding:20px 0 10px 0" colspan='3'>Сменить пароль</td>
					</tr>
					<tr>
						<td class="tLabel"><label for="oldpass">Старый пароль</label></td>
						<td class="tInput"><input type="password" class='rBorder' name="oldpass" id="oldpass" /><?php
							if ($route->GET("password") == "oldpass")
								echo "<p class='inf bad'>неверный старый пароль<p>";
							?></td>
						<td class="tButton"></td>
					</tr>
					<tr>
						<td class="tLabel"><label for="newpass">Новый пароль</label></td>
						<td class="tInput"><input type="password" class='rBorder' name="newpass" id="newpass" /></td>
						<td class="tButton"></td>
					</tr>
					<tr>
						<td class="tLabel"><label for="newpass_repeat">Повтор пароля</label></td>
						<td class="tInput"><input type="password" class='rBorder' name="newpass_repeat" id="newpass_repeat" /><?php
							if ($route->GET("password") == "repeat")
								echo "<p class='inf bad'>Пароль и повтор не сопадают<p>";
							elseif ($route->GET("password") == "success")
								echo "<p class='inf good'>Пароль изменен<p>";
							?></td>
						<td class="tButton"><input type="submit" value="Изменить"></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</div>

<?php require_once CABINET.DS."footer.php"; ?>
